/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

package org.erossi.pigest.web.documento;

import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.erossi.pigest.model.articolo.Articolo;
import org.erossi.pigest.model.documento.Documento;
import org.erossi.pigest.model.documento.RigaDocumento;
import org.erossi.pigest.model.documento.TipoDocumento;
import org.erossi.pigest.model.soggetto.Soggetto;
import org.springframework.web.servlet.view.document.AbstractExcelView;

/**
 * 'ListaDocumentiHSSFView' class: classe che costituisce la vista per la versione HSSF
 *   della lista documenti.
 *
 * @author erossi
 * @version Sep 24, 2008
 */
public class ListaDocumentiHSSFView extends AbstractExcelView {

  @Override
  @SuppressWarnings("unchecked")
  protected void buildExcelDocument(Map model, HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception {

    HSSFCellStyle dateCellStyle = workbook.createCellStyle();
    dateCellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
    
    HSSFCellStyle currencyCellStyle = workbook.createCellStyle();
    currencyCellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("#,##0.00"));
    
    HSSFSheet sheet_0 = workbook.createSheet(this.getApplicationContext().getMessage("bean.documents", null, request.getLocale()));

    HSSFRow row_0 = sheet_0.createRow(0);

    HSSFCell cell_r0c0 = row_0.createCell((short) 0);
    cell_r0c0.setCellValue(this.getApplicationContext().getMessage("bean.documents.id", null, request.getLocale()));
    HSSFCell cell_r0c1 = row_0.createCell((short) 1);
    cell_r0c1.setCellValue(this.getApplicationContext().getMessage("bean.documents.version", null, request.getLocale()));
    HSSFCell cell_r0c2 = row_0.createCell((short) 2);
    cell_r0c2.setCellValue(this.getApplicationContext().getMessage("bean.documents.open", null, request.getLocale()));  
    HSSFCell cell_r0c3 = row_0.createCell((short) 3);
    cell_r0c3.setCellValue(this.getApplicationContext().getMessage("bean.documents.from", null, request.getLocale()));
    HSSFCell cell_r0c4 = row_0.createCell((short) 4);
    cell_r0c4.setCellValue(this.getApplicationContext().getMessage("bean.documents.to", null, request.getLocale()));
    HSSFCell cell_r0c5 = row_0.createCell((short) 5);
    cell_r0c5.setCellValue(this.getApplicationContext().getMessage("bean.documents.type", null, request.getLocale()));
    HSSFCell cell_r0c6 = row_0.createCell((short) 6);
    cell_r0c6.setCellValue(this.getApplicationContext().getMessage("bean.documents.document_date", null, request.getLocale()));
    HSSFCell cell_r0c7 = row_0.createCell((short) 7);
    cell_r0c7.setCellValue(this.getApplicationContext().getMessage("bean.documents.load_date", null, request.getLocale()));
    HSSFCell cell_r0c8 = row_0.createCell((short) 8);
    cell_r0c8.setCellValue(this.getApplicationContext().getMessage("bean.documents.description", null, request.getLocale()));
    HSSFCell cell_r0c9 = row_0.createCell((short) 9);
    cell_r0c9.setCellValue(this.getApplicationContext().getMessage("bean.documents.notes", null, request.getLocale()));
    HSSFCell cell_r0c10 = row_0.createCell((short) 10);
    cell_r0c10.setCellValue(this.getApplicationContext().getMessage("bean.document_rows.id", null, request.getLocale()));
    HSSFCell cell_r0c11 = row_0.createCell((short) 11);
    cell_r0c11.setCellValue(this.getApplicationContext().getMessage("bean.document_rows.version", null, request.getLocale()));
    HSSFCell cell_r0c12 = row_0.createCell((short) 12);
    cell_r0c12.setCellValue(this.getApplicationContext().getMessage("bean.document_rows.open", null, request.getLocale()));
    HSSFCell cell_r0c13 = row_0.createCell((short) 13);
    cell_r0c13.setCellValue(this.getApplicationContext().getMessage("bean.document_rows.item", null, request.getLocale()));
    HSSFCell cell_r0c14 = row_0.createCell((short) 14);
    cell_r0c14.setCellValue(this.getApplicationContext().getMessage("bean.document_rows.description", null, request.getLocale()));
    HSSFCell cell_r0c15 = row_0.createCell((short) 15);
    cell_r0c15.setCellValue(this.getApplicationContext().getMessage("bean.document_rows.quantity", null, request.getLocale()));
    HSSFCell cell_r0c16 = row_0.createCell((short) 16);
    cell_r0c16.setCellValue(this.getApplicationContext().getMessage("bean.document_rows.unit_price", null, request.getLocale()));
    HSSFCell cell_r0c17 = row_0.createCell((short) 17);
    cell_r0c17.setCellValue(this.getApplicationContext().getMessage("bean.document_rows.notes", null, request.getLocale()));
    
    List<Documento> documenti = (List<Documento>) model.get("listaDocumenti");
    
    if (documenti != null) {
    
      for (int i = 0; i < documenti.size(); i++) {
        
        Documento documento_i = documenti.get(i);
        Set<RigaDocumento> righe = documento_i.getRighe();
        
        for (RigaDocumento riga_j : righe) {
          
          HSSFRow row_k = sheet_0.createRow(sheet_0.getLastRowNum() + 1);
          
          HSSFCell cell_rKc0 = row_k.createCell((short) 0);
          Long documento_id = documento_i.getId();
          if (documento_id != null) {cell_rKc0.setCellValue(documento_id);}
          
          HSSFCell cell_rKc1 = row_k.createCell((short) 1);
          Long documento_versione = documento_i.getVersion();
          if (documento_versione != null) {cell_rKc1.setCellValue(documento_versione);}
          
          HSSFCell cell_rKc2 = row_k.createCell((short) 2);
          Boolean documento_aperto = documento_i.getAperto();
          if (documento_aperto != null) {
            if (documento_aperto) {
              cell_rKc2.setCellValue(this.getApplicationContext().getMessage("general.open", null, request.getLocale()));
            } else {
              cell_rKc2.setCellValue(this.getApplicationContext().getMessage("general.close", null, request.getLocale()));
            }
          }
          
          HSSFCell cell_rKc3 = row_k.createCell((short) 3);
          Soggetto documento_mittente = documento_i.getMittente();
          if (documento_mittente != null) {
            Long idMittente = documento_mittente.getId();
            if (idMittente != null) {cell_rKc3.setCellValue(idMittente);}
          }
          
          HSSFCell cell_rKc4 = row_k.createCell((short) 4);
          Soggetto documento_destinatario = documento_i.getDestinatario();
          if (documento_destinatario != null) {
            Long idDestinatario = documento_destinatario.getId();
            if (idDestinatario != null) {cell_rKc4.setCellValue(idDestinatario);}
          }
          
          HSSFCell cell_rKc5 = row_k.createCell((short) 5);
          TipoDocumento documento_tipo = documento_i.getTipo();
          if (documento_tipo != null) {
            Long idTipoDocumento = documento_tipo.getId();
            if (idTipoDocumento != null) {cell_rKc5.setCellValue(idTipoDocumento);}
          }          
          
          HSSFCell cell_rKc6 = row_k.createCell((short) 5);
          cell_rKc6.setCellStyle(dateCellStyle);
          Calendar documento_dataDocumento = documento_i.getDataDocumento();
          if (documento_dataDocumento != null) {cell_rKc6.setCellValue(documento_dataDocumento);}    
          
          HSSFCell cell_rKc7 = row_k.createCell((short) 7);
          cell_rKc6.setCellStyle(dateCellStyle);
          Calendar documento_dataCarico = documento_i.getDataCarico();
          if (documento_dataCarico != null) {cell_rKc7.setCellValue(documento_dataCarico);}    
          
          HSSFCell cell_rKc8 = row_k.createCell((short) 8);
          String documento_descrizione = documento_i.getDescrizione();
          if (documento_descrizione != null) {cell_rKc8.setCellValue(documento_descrizione);}
          
          HSSFCell cell_rKc9 = row_k.createCell((short) 9);
          String documento_note = documento_i.getNote();
          if (documento_note != null) {cell_rKc9.setCellValue(documento_note);}
          
          HSSFCell cell_rKc10 = row_k.createCell((short) 10);
          Long riga_id = riga_j.getId();
          if (riga_id != null) {cell_rKc10.setCellValue(riga_id);}
          
          HSSFCell cell_rKc11 = row_k.createCell((short) 11);
          Long riga_versione = riga_j.getVersion();
          if (riga_versione != null) {cell_rKc11.setCellValue(riga_versione);}
          
          HSSFCell cell_rKc12 = row_k.createCell((short) 12);
          Boolean riga_aperto = riga_j.getAperto();
          if (riga_aperto != null) {
            if (riga_aperto) {
              cell_rKc12.setCellValue(this.getApplicationContext().getMessage("general.open", null, request.getLocale()));
            } else {
              cell_rKc12.setCellValue(this.getApplicationContext().getMessage("general.close", null, request.getLocale()));
            }
          }
          
          HSSFCell cell_rKc13 = row_k.createCell((short) 13);
          Articolo riga_articolo = riga_j.getArticolo();
          if (riga_articolo != null) {
            Long idArticolo = riga_articolo.getId();
            if (idArticolo != null) {cell_rKc13.setCellValue(idArticolo);}
          }
          
          HSSFCell cell_rKc14 = row_k.createCell((short) 14);
          String riga_descrizione = riga_j.getDescrizione();
          if (riga_descrizione != null) {cell_rKc14.setCellValue(riga_descrizione);}
          
          HSSFCell cell_rKc15 = row_k.createCell((short) 15);
          Integer riga_quantita = riga_j.getQuantita();
          if (riga_quantita != null) {cell_rKc15.setCellValue(riga_quantita);}
          
          HSSFCell cell_rKc16 = row_k.createCell((short) 16);
          cell_rKc14.setCellStyle(currencyCellStyle);
          Double riga_prezzoUnitario = riga_j.getPrezzoUnitario();
          if (riga_prezzoUnitario != null) {cell_rKc16.setCellValue(riga_prezzoUnitario);}
          
          HSSFCell cell_rKc17 = row_k.createCell((short) 17);
          String riga_note = riga_j.getNote();
          if (riga_note != null) {cell_rKc17.setCellValue(riga_note);}
          
        }
        
      }
    
    }
    
  }
  
}

